/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QPainter>

#include "timerlistdelegate.h"
#include "timerlistmodel.h"


void TimerListDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option,
                                 const QModelIndex &index ) const
{
    bool active        = index.model()->data( index, TimerListModel::TimerActiveRole ).toBool();
    bool recording     = index.model()->data( index, TimerListModel::TimerRecordingRole ).toBool();
//     bool pending       = index.model()->data( index, TimerListModel::TimerPendingRole ).toBool();
    QString file       = index.model()->data( index, TimerListModel::TimerFileRole ).toString();
    QString startTime  = index.model()->data( index, TimerListModel::TimerStartTimeRole ).toDateTime().toString( "dd.MM.  hh:mm" );
    QString stopTime   = index.model()->data( index, TimerListModel::TimerStopTimeRole ).toDateTime().toString( "hh:mm" );

// qDebug() << "TimerListDelegate::paint for file:" << file << "   day: " << index.model()->data( index, TimerListModel::TimerDayRole ) ;

    QString directory;

    int pos = file.indexOf( '~', 0 );
    if ( pos )
    {
        directory = file.left( pos );
        file = file.remove( 0, pos + 1 );
        file.replace( "~", " / " );
    }

    painter->save();

    /* No idea yet, when it is not active */
    if ( !active )
    {
        painter->fillRect( option.rect, option.palette.mid() );
    }

    /* Selected uses a different color */
    if ( option.state & QStyle::State_Selected )
    {
        painter->fillRect( option.rect, option.palette.highlight() );
        painter->setPen( option.palette.highlightedText().color() );
    }

    int x = option.rect.left();
    int y = option.rect.top();
    int width = option.rect.width();
//     int height = option.rect.height();
    QRect rect;

    /* Currently recording */
    if ( recording )
    {
        rect = QRect( x + 3, y + 6, 16, 16 );
        painter->fillRect( rect, Qt::red );
        painter->drawText( rect, Qt::AlignCenter, "R" );
    }

    /* Pending means should record but no card available */
//     if ( pending )
//     {
//         rect = QRect( x + 3, y + 6, 16, 16 );
//         painter->fillRect( rect, Qt::black );
//     }

    /* Time */
    rect = QRect( x + 22, y + 6, 132, 22 );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, startTime + "-" + stopTime );

    /* file */
    rect = QRect( x + 154, y + 6, width - 154, 22 );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, file );

    /* Directory */
    rect = QRect( width - 150, y + 6, 146, 22 );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, directory );

    painter->restore();
}


QSize TimerListDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    Q_UNUSED( option );
    Q_UNUSED( index );

    return QSize( 100, 48 );
}
